package cos.android.mapfog;

import java.io.IOException;
import java.util.List;
import java.util.Locale;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.location.Address;
import android.location.Criteria;
import android.location.Geocoder;
import android.location.GpsStatus;
import android.location.GpsStatus.Listener;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.os.Bundle;
import android.view.View.OnCreateContextMenuListener;
import android.widget.TextView;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

public class FogMap extends MapActivity implements Listener, LocationListener {
	/** Called when the activity is first created. */

	LocationManager mLocationManager;
	LocationProvider mLocation;
	TextView tv;
	MapView mapView;

	List<Overlay> mapOverlays;
	Drawable drawable;
	ItemsOverlay itemizedOverlay;


	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		mapView = (MapView) findViewById(R.id.map1);
		mapView.setBuiltInZoomControls(true);
		tv = (TextView) findViewById(R.id.tv1);
		mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
		Criteria criteria = new Criteria();
		criteria.setAccuracy(Criteria.ACCURACY_FINE);
		criteria.setPowerRequirement(Criteria.POWER_LOW);
		/*
		 * String locationprovider = mLocationManager.getBestProvider(criteria,
		 * true);
		 */
		mLocation = mLocationManager.getProvider(mLocationManager.GPS_PROVIDER);

		mapOverlays = mapView.getOverlays();
		int level = mapView.getZoomLevel();
		drawable = this.getResources().getDrawable(R.drawable.lander_plain);
		itemizedOverlay = new ItemsOverlay(drawable);

		GeoPoint point = new GeoPoint(19240000, -99120000);
		OverlayItem overlayitem = new OverlayItem(point, "", "");

		MyLocationOverlay myLocation = new MyLocationOverlay(
				getApplicationContext(), mapView);

		itemizedOverlay.addOverlay(overlayitem);
		mapOverlays.add(itemizedOverlay);

		mLocationManager.addGpsStatusListener(this);

		mLocationManager.requestLocationUpdates(mLocationManager.GPS_PROVIDER,
				0, 0, this);

		/*
		 * GeoPoint point2 = myLocation.getMyLocation();
		 * 
		 * System.out.println("Point2: " + point2.toString());
		 */

		/*
		 * Location loc = mLocationManager
		 * .getLastKnownLocation(LocationManager.GPS_PROVIDER);
		 * 
		 * System.out.println("Lat: " + loc.getLatitude() + " Lon: " +
		 * loc.getLongitude());
		 * 
		 * List<Address> addresses; try { Geocoder mGC = new Geocoder(this,
		 * Locale.ENGLISH); addresses = mGC.getFromLocation(loc.getLatitude(),
		 * loc.getLongitude(), 1); if (addresses != null) { Address currentAddr
		 * = addresses.get(0); StringBuilder mSB = new
		 * StringBuilder("Address:\n"); for (int i = 0; i <
		 * currentAddr.getMaxAddressLineIndex(); i++) {
		 * mSB.append(currentAddr.getAddressLine(i)).append("\n"); }
		 * System.out.println((mSB.toString())); } } catch (IOException e) {
		 * System.out.println(e.getMessage()); }
		 */

	}

	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void onGpsStatusChanged(int event) {
		System.out.println("Cambio a: " + event);
		GpsStatus status = mLocationManager.getGpsStatus(null);
		System.out.println(status.toString());

	}

	@Override
	public void onLocationChanged(Location location) {
		System.out.println("onLocationChanged");
	}

	@Override
	public void onProviderDisabled(String provider) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onProviderEnabled(String provider) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onStatusChanged(String provider, int status, Bundle extras) {
		// TODO Auto-generated method stub

	}
}
